package com.annie.boots.service;

import com.annie.boots.bean.Product;
import com.annie.boots.mapper.ProductMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    public Product getProductById(Integer id){
        return productMapper.getProduct(id);
    }


    public List<Product> getListByName(String productName){
        log.info("*****************************productName"+productName);
        return productMapper.getProductByName(productName);
    }

    @Override
    public List<Product> findAllProductByPageF(String productName,int pageNum, int pageSize) {
        // TODO Auto-generated method stub
        PageHelper.startPage(pageNum, pageSize);
        List<Product> lists = productMapper.selectProductByPage(productName);
        return lists;
    }

    @Override
    public PageInfo<Product> findAllProductByPageS(String productName,int pageNum, int pageSize) {
        // TODO Auto-generated method stub
        PageHelper.startPage(pageNum, pageSize);
        List<Product> lists = productMapper.selectProductByPage(productName);
        PageInfo<Product> pageInfo = new PageInfo<Product>(lists);
        return pageInfo;

    }

    @Override
    public boolean delProduct(Integer id)  {
        int iRes = 0;
        try {
            iRes = productMapper.delProduct(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(iRes>0){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public boolean addProduct(Product product) {
        if (productMapper.addProduct(product)>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean updateProduct(Product product) {
        if (productMapper.updateProduct(product)>0){
            return true;
        }else{
            return false;
        }
    }
}
